package myJs.data.detailInfo

import myJs.I18n
import myJs.Implicits._
import myJs.components.SummaryComponent
import myJs.data.{History, _}
import myJs.myPkg.jquery._
import myJs.tool.Tool
import org.scalajs.dom._
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.scalajs.js
import scala.scalajs.js.Dynamic.{global => g}
import scala.scalajs.js.JSON
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}

/** Created by yz on 16/9/2021
  */
@JSExportTopLevel("DetailInfo")
object DetailInfo {

  @JSExport("init")
  def init = {
    refreshAnnoInfo(Tool.getRsNumber)
      .map { x =>
        val id = window.location.href.split("#").last
        if (id.nonBlank) {
          val target_top = $(s"#${id}").offset().top
          $("html,body").animate(js.Dictionary("scrollTop" -> target_top), JQueryAnimationSettings.duration(800))
        }
      }
    registerEvent
    I18n.initialize()
    Cp.Component().renderIntoDOM(document.querySelector("#root"))
    GeneCp.Props(symbol = Tool.getSymbol).render.renderIntoDOM(document.querySelector("#gene"))
  }

  def filterContentShowFront(y: Element) = {
    $(s".filterContent").hide()
    val fieldName = Tool.getFilterDataFieldName(y)
    val contentJq = $(y).parents(".table-responsive").find(s"[id='${fieldName}Content']")
    val yTop = $(y).offset().top + $(y).height() + 3
    val pageWidth = document.body.clientWidth
    val yTotalLeft = $(y).offset().left
    val leftWidth = $(".container").css("margin-left").px2Double -
      15
    val yLeft = yTotalLeft - leftWidth
    val width = 210
    val trueYLeft = if (yTotalLeft + width > pageWidth) pageWidth - leftWidth - 220 - 6 else yLeft
    contentJq.css("top", yTop.toInt).css("left", trueYLeft.toInt).width(width)
    contentJq.show()
    Tool.filterContentFillBeforeValue(contentJq)
  }

  def refreshAnnoInfo(rsNumber: String) = {
    val url = g.jsRoutes.controllers.AnnoController.getJson().url.toString
    Ajax.get(url = s"${url}?rsNumber=${rsNumber}").map { xhr =>
      val dict = JSON.parse(xhr.responseText).toJDJAn
      if (dict != null) {
        val state = SummaryComponent.State.init.copy(summaryDict = dict("summary").toJDJAn)
        SummaryComponent.component(state)().renderIntoDOM(document.querySelector(s"#summary"))
        val detailDicts = dict("detail").toJArJDJAn
        Details.init(detailDicts)
        val geneDetailDicts = dict("geneDetail").toJArJDJAn
        GeneDetail.init(geneDetailDicts)
        val significanceDicts = dict("significances").toJArJDJAn
        Significance.init(significanceDicts)
        val alfaDicts = dict("alfa").toJArJDJAn
        AlfaFrequency.init(alfaDicts)
        val frequencyDicts = dict("frequency").toJArJDJAn
        Frequency.init(frequencyDicts)
        val hgvsDicts = dict("hgvs").toJArJDJAn
        Hgvs.init(hgvsDicts)
        val submissionDicts = dict("submission").toJArJDJAn
        Submissions.init(submissionDicts)
        val historyDicts = dict("history").toJArJDJAn
        History.init(historyDicts)
        val addedHistoryDicts = dict("addedHistory").toJArJDJAn
        AddedHistory.init(addedHistoryDicts)
        val publicationDicts = dict("publication").toJArJDJAn
        Publications.init(publicationDicts)
        $("#anno").show()
      } else {
        $("#anno").hide()
      }

    }
  }

  def registerEvent = {}

}
